/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.verolux.soft.debaraz.service;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.FirstBean;
import com.verolux.soft.debaraz.dao.FirstDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author ut1e8p
 */
@Service(value="firstService")
public class FirstServiceImpl implements FirstService {

    @Autowired
    private FirstDAO firstDAO;

    /**
     *
     * @param bean
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ApplicationException.class)
    public void insert(FirstBean bean) throws ApplicationException {
        try {
            if (bean.getId() != null) {
                bean.setId(null);
            }
            firstDAO.save(bean);
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     * 
     * @param bean
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ApplicationException.class)
    public void update(FirstBean bean) throws ApplicationException {
        try {
            if (bean.getId() == null) {
                throw new ApplicationException("Null Id");
            }
            firstDAO.save(bean);
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     * 
     * @param id
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ApplicationException.class)
    public void deleteById(Long id) throws ApplicationException {
        try {
            FirstBean bean = findById(id);
            if (bean != null) {
                firstDAO.delete(bean);
            }
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     * 
     * @param id
     * @return
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public FirstBean findById(Long id) throws ApplicationException {
        try {
            return firstDAO.findById(id);
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }
}
